#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>a
#include <time.h>

#define WIDTH 40
#define HEIGHT 20

enum DIRECTION{ STOP=0,LEFT=4,RIGHT=6,UP=8,DOWN=2};

typedef struct Snake{
    int x;
    int y;
    struct Snake*next;
}Snake;

int score;
int gameover;
enum DIRECTION dir;
Snake*head;
int foodX,foodY;

void Setup(){
    gameover=0;
    dir=STOP;
    score=0;
//初始化蛇
    head=(Snake*)malloc(sizeof(Snake));
    head->x=WIDTH/2;
    head->y=HEIGHT/2;
    head->next=NULL;

    //食物
    srand(time(NULL));
    foodX=rand()%(WIDTH-2)+1;
    foodY=rand()%(HEIGHT-2)+1;

}

//游戏界面
void Draw(){
    system("cls");//清屏


    //绘制上边框
    for (int i=0;i<WIDTH;i++)
       printf("#");
    printf("\n");
    
    //绘制游戏界面
    for (int y=0;y<HEIGHT;y++){
        for(int x=0;x<WIDTH;x++){
            if(x==0||x==WIDTH-1)
              printf("#");//左右边框
            else if(y == foodY && x==foodX)
              printf("F"); //食物
            else{

                Snake*current=head;
                int isbody=0;
                while (current != NULL){
                    if(current->x == x && current->y ==y){
                        printf("0");
                        isbody=1;
                        break;
                    }
                    current=current->next;   
                }
                if(!isbody)printf("");
            }
        }
        printf("\n");
    }

    //绘制下边框
    for (int i=0; i<WIDTH;i++)
        printf("#");
    printf("\n");

    printf("Score:%d\n",score);
}
//处理输入
void Input(){
    if (_kbhit()){   //检测键盘输入
        switch (_getch()) {
            case 'a': dir=LEFT; break;
            case 'd': dir=RIGHT; break;
            case 'w': dir=UP; break;
            case 's': dir=DOWN; break;
            case 'x': gameover=1; break;   //按x退出
            
        }
    }
}
//移动蛇
void Move(){
    //保存之前的尾部用于增长;
    Snake *oldTail= head;
    while(oldTail->next != NULL)oldTail=oldTail->next;

    //移动身体
    Snake*prev=NULL;
    Snake*current=head;
    while(current->next != NULL){
        prev=current;
        current=current->next;
        prev->x=current->x;
        prev->y=current->y;
        }

    //移动头部
    switch(dir){
        case LEFT:  head->x--; break;
        case RIGHT: head->x++; break;
        case UP:    head->y--; break;
        case DOWN:  head->y++; break;
    }

    //检查是否吃到食物
    if(head->x ==foodX && head->y ==foodY){
        score += 10;
        //生成新事物
        foodX= rand() %(WIDTH  -2)+ 1;
        foodY= rand() %(HEIGHT -2)+ 1;
        //增长蛇长度
        Snake*newNode = (Snake*)malloc(sizeof(Snake));
        newNode->x= oldTail->x;
        newNode->y= oldTail->y;
        newNode->next= NULL;
        oldTail->next = newNode;

    }
    
    //碰撞检测(撞墙或自身)
    if (head->x <=0|| head->x >= WIDTH-1 ||
        head->y <0 || head->y >= HEIGHT) {
            gameover =1;
        }

        current = head->next;
        while(current !=NULL){
            if (head->x ==current->x && head->y == current->y){
                gameover = 1;
                break;
            }
            current = current->next;

        }
}

int main(){
    Setup();
    while (!gameover){
        Draw();
        Input();
        Move();
        Sleep(100);//控制游戏速度
    }
    printf("Game Over! Final Score: %d\n",score);
    return 0;
}

    
